#include <typescommon.h>
// int param_LogLevel = 4; // reference logLevelEnum

size_t memcpypro(void *dest, size_t destsize, const void *src, size_t srcsize)
{
    destsize = destsize > srcsize ? srcsize : destsize;
    if (!dest || !src || destsize < 1)
    {
        return 0;
    }
    memcpy(dest, src, destsize);
    return destsize;
}
#if DEVICE_PLATFORM_CONFIGURE != DEVICE_PLATFORM_C51_MCU
size_t memcpyStr(void *dest, size_t destsize, const char *src)
{
    return memcpypro(dest, destsize, src, strlen(src) + 1);
}
#endif

sysTmrType systemNowMs()
{
#if DEVICE_PLATFORM_CONFIGURE == DEVICE_PLATFORM_LINUX
    struct timespec tnow;
    clock_gettime(CLOCK_MONOTONIC, &tnow);
    return (sysTmrType)(tnow.tv_sec) * 1000 + tnow.tv_nsec / 1000000;
#else
    return 0xc721b40a;
#endif
}

#if DEVICE_PLATFORM_CONFIGURE == DEVICE_PLATFORM_LINUX
int clogCommon(int lev, const char *lfile, int lline, const char *fmt, ...)
{
    if (lev > parmNormIstc.loglev)
    {
        return 0;
    }
    static const char *logstr[] = {
        "fat",
        "err",
        "wrn",
        "inf",
        "dbg",
        "trc"};
    struct timeval tnow;
    struct tm *tmnow;
    gettimeofday(&tnow, NULL);
    tmnow = localtime(&tnow.tv_sec);
    char tmpPrtStr[2048] = {0};
    const char *tmpFileName;
    (tmpFileName = strrchr(lfile, '/')) ? tmpFileName++ : (tmpFileName = lfile);
    int tmplen = snprintf(tmpPrtStr, sizeof(tmpPrtStr), "[%d-%02d-%02d %02d:%02d:%02d.%03d]%s[%s,%d]",
                          tmnow->tm_year + 1900, tmnow->tm_mon + 1, tmnow->tm_mday, tmnow->tm_hour, tmnow->tm_min, tmnow->tm_sec,
                          (int)(tnow.tv_usec / 1000 % 1000), logstr[lev], tmpFileName, lline);
    va_list args;
    va_start(args, fmt);
    tmplen += vsnprintf(tmpPrtStr + tmplen, sizeof(tmpPrtStr) - tmplen, fmt, args);
    va_end(args);
    printf("%s\n", tmpPrtStr);
    return tmplen;
}
#elif DEVICE_PLATFORM_CONFIGURE == DEVICE_PLATFORM_ARM32_MCU
int clogCommon(int lev, const char *lfile, int lline, const char *fmt, ...)
{
    if (lev > parmNormIstc.loglev)
    {
        return 0;
    }
    char tmpPrtStr[256] = {0};
    const char *tmpFileName;
    (tmpFileName = strrchr(lfile, '/')) ? tmpFileName++ : (tmpFileName = lfile);
    int tmplen = snprintf(tmpPrtStr, sizeof(tmpPrtStr), "[%s,%d]", tmpFileName, lline);
    va_list args;
    va_start(args, fmt);
    tmplen += vsnprintf(tmpPrtStr + tmplen, sizeof(tmpPrtStr) - tmplen, fmt, args);
    va_end(args);
    printf("%s\n", tmpPrtStr);
    return tmplen;
}
#else
// void logfatal(const char *fmt, ...)
// {
//     fmt = NULL;
// }
void logerror(const char *fmt, ...)
{
    fmt = NULL;
}
// void logwarn(const char *fmt, ...)
// {
//     fmt = NULL;
// }
// void loginform(const char *fmt, ...)
// {
//     fmt = NULL;
// }
void logdebug(const char *fmt, ...)
{
    fmt = NULL;
}
// void logtrace(const char *fmt, ...)
// {
//     fmt = NULL;
// }
#endif
